package com.review.study;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 *服务能同时接受客户端的信息并且能再发送信息给客户端吗？
 */
public class ServerSocketOperation {
    static ServerSocket serverSocket;
    private static Socket socket;

    public static void main(String[] args) {
        {
            try {
                serverSocket = new ServerSocket(8769);
                while (Boolean.TRUE) {
                    System.out.println("服务器已经启动");
                    //方法是阻塞的，就是没有数据来的时候，方法会不向下执行
                    Thread.sleep(1000);
                    socket = serverSocket.accept();
                    System.out.println(socket.getInetAddress() + "" + socket.getPort());
                    InputStream inputStream = socket.getInputStream();
                    byte[] bytes = new byte[5];
                    inputStream.read(bytes);
                    System.out.println(new String(bytes));
                    socket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
